package com.opensource.svgaplayer;

import ohos.agp.utils.TextTool;
import ohos.app.Context;
import java.io.File;
import java.net.URL;
import java.security.MessageDigest;

/**
 * ex.
 *
 * @author wl
 * @since 2021-06-28
 */
public class SVGACache {
    private Type type;
    private String cacheDir;
    private static volatile SVGACache INSTANCE;

    /**
     * getInstance
     *
     * @return SVGACache
     */
    public static SVGACache getInstance() {
        if (INSTANCE == null) {
            synchronized (SVGACache.class) {
                INSTANCE = new SVGACache();
            }
        }
        return INSTANCE;
    }

    /**
     * onCreate
     *
     * @param context Context
     */
    public void onCreate(Context context) {
        this.onCreate(context, Type.DEFAULT);
    }

    /**
     * onCreate
     *
     * @param context Context
     * @param type    SVGACache.Type
     */
    public void onCreate(Context context, SVGACache.Type type) {
        if (isInitialized()) {
            return;
        }
        if (context == null) {
            return;
        }
        cacheDir = context.getCacheDir().getAbsolutePath() + "\"/svga/\"";
        File file = new File(cacheDir);
        if (!file.exists()) {
            boolean mkdirs = file.mkdirs();
            if (!mkdirs){
                throw new IllegalArgumentException("make dirs fail");
            }
        }
        this.type = type;
    }

    /**
     * isInitialized
     *
     * @return boolean
     */
    public boolean isInitialized() {
        return !("/".equals(cacheDir));
    }

    /**
     * isDefaultCache
     *
     * @return boolean
     */
    public boolean isDefaultCache() {
        return type == Type.DEFAULT;
    }

    /**
     * isCached
     *
     * @param cacheKey String
     * @return boolean
     */
    public boolean isCached(String cacheKey) {
        if (cacheKey == null) {
            return false;
        }
        return (this.isDefaultCache() ? this.buildCacheDir(cacheKey) : this.buildSvgaFile(cacheKey)).exists();
    }

    /**
     * buildCacheKey
     *
     * @param str String
     * @return String
     */
    public String buildCacheKey(String str) {
        StringBuffer sb = new StringBuffer("");
        if (TextTool.isNullOrEmpty(str)) {
            return sb.toString();
        }
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            messageDigest.update(str.getBytes("UTF-8"));
            byte[] digest = messageDigest.digest();
            for (byte b : digest) {
//                sb += String.format("%02x", b);
                sb.append(String.format("%02x", b));
            }
        } catch (Exception e) {

        }
        return sb.toString();
    }

    /**
     * buildCacheKey
     *
     * @param url URL
     * @return String
     */
    public String buildCacheKey(URL url) {
        String strUrl = "";
        if (url != null) {
            strUrl = buildCacheKey(url.toString());
        }
        return strUrl;
    }

    /**
     * buildCacheDir
     *
     * @param cacheKey String
     * @return File
     */
    public File buildCacheDir(String cacheKey) {

        if (TextTool.isNullOrEmpty(cacheKey)) {
            return null;
        }
        return new File(cacheDir + cacheKey + "/");
    }

    /**
     * buildSvgaFile
     *
     * @param cacheKey String
     * @return File
     */
    public File buildSvgaFile(String cacheKey) {
        if (TextTool.isNullOrEmpty(cacheKey)) {
            return null;
        }
        return new File(cacheDir + cacheKey + ".svga");
    }

    /**
     * buildAudioFile
     *
     * @param audio String
     * @return File
     */
    public File buildAudioFile(String audio) {
        if (TextTool.isNullOrEmpty(audio)) {
            return null;
        }
        return new File(cacheDir + audio + ".mp3");
    }

    private SVGACache() {
        type = Type.DEFAULT;
        cacheDir = "/";
    }

    public static enum Type {
        DEFAULT,
        FILE;
    }
}
